package com.ruoyi.web.controller.resturant;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BizException;
import com.ruoyi.resturant.domain.ResShop;
import com.ruoyi.resturant.domain.Student;
import com.ruoyi.resturant.service.IStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Anonymous
@RestController
@RequestMapping("/api/resturant/student")
@Api(tags = "学生信息管理")
public class StudentController extends BaseController {
    @Autowired
    private IStudentService studentService;
    @Autowired
    private RedisCache redisCache;

    @ApiOperation("取得所有的学生列表")
    @GetMapping("/list")
    public AjaxResult getStudents(){
        List<Student> rs = studentService.getStudents();
        return AjaxResult.success(rs);
    }
    /**
     * 获取学生详细信息
     */
    @ApiOperation(value = "获取学生详细信息", notes = "TODO：取得某个学生的详细信息")
    @ApiImplicitParam(name = "id", value = "学生ID",
            required = true, dataType = "long", paramType = "path",
            dataTypeClass = Long.class)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        // 1 根据店铺ID从redis取得学生详情的json字符串
        String str = redisCache.getCacheObject(String.valueOf(id));
        // 2 把店铺详情的json字符串转换为一个model对象ResShop
        Student student = JSON.parseObject(str, Student.class);

        if(student == null) {
            student = studentService.selectStudentById(id);
            if (student == null){
                throw  new BizException(404,"该学生id("+id+")不存在");
            }
            String strDB = JSON.toJSONString(student);
            redisCache.setCacheObject(String.valueOf(id), strDB,
                    1, TimeUnit.HOURS);
        }

        // 3 返回取得model对象
        return AjaxResult.success(student);
    }
    /**
     * 新增学生
     */
    @Log(title = "学生管理",businessType = BusinessType.INSERT)
    @ApiOperation("新增学生")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Student student) {
        return AjaxResult.success(studentService.insertStudent(student));
    }

    /**
     * 修改店学生
     */
    @Log(title = "学生管理",businessType = BusinessType.UPDATE)
    @ApiOperation("修改学生")
    @PutMapping
    public AjaxResult edit( @RequestBody Student student) {

        Student studentDB = studentService.selectStudentById(student.getId());

        if (studentDB == null){
            throw new BizException(404,"该学生id("+student.getId()+")不存在");
        }

        return AjaxResult.success(studentService.updateStudent(student));
    }

    /**
     * 删除学生
     */
    @Log(title = "学生管理",businessType = BusinessType.DELETE)
    @ApiOperation("删除学生")
    @ApiImplicitParam(name = "id", value = "店铺ID", required = true, dataType = "long",
            paramType = "path", dataTypeClass = Long.class)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return AjaxResult.success(studentService.selectStudentById(id));
    }

}